package com.easy.generator.java;

import com.easy.generator.Easy;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDate;

/**
 * @author easy
 * @date 2025年07月05日 17:27
 */
public class PasswordGenerator {
    private static final String CLASS_NAME = "PasswordDecode";
    private static final String CLASS_TEMPLATE =
                            "package "+ Easy.UTIL_PACKAGE+";\n"+
                            "import cn.hutool.crypto.digest.DigestUtil;\n" +
                            "\n" +
                            "public class "+CLASS_NAME+" {\n" +
                            "    private static int[] char_arr={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};\n" +
                            "    public static String encryption(String pass){\n" +
                            "        //加密次数\n" +
                            "        int index_count=(int)(Math.random()*char_arr.length);\n" +
                            "        //加密盐值\n" +
                            "        int index_salt=(int)(Math.random()*char_arr.length);\n" +
                            "        String result=pass;\n" +
                            "        for(int i=0;i<char_arr[index_count];i++){\n" +
                            "            result=result+(char)char_arr[index_salt];\n" +
                            "            result= DigestUtil.md5Hex(result);\n" +
                            "        }\n" +
                            "        result=(char)char_arr[index_count]+result+(char)char_arr[index_salt];\n" +
                            "        return result;\n" +
                            "    }\n" +
                            "\n" +
                            "    public static void main(String[] args) {\n" +
                            "        System.out.println(encryption(\"123123\"));\n" +
                            "    }\n" +
                            "    \n" +
                            "    public static boolean verify(String pass,String decode){\n" +
                            "        //加密次数\n" +
                            "        char char_count=decode.charAt(0);\n" +
                            "        //加密盐值\n" +
                            "        char char_salt=decode.charAt(decode.length()-1);\n" +
                            "\n" +
                            "        for(int i=0;i<char_count;i++){\n" +
                            "            pass=pass+char_salt;\n" +
                            "            pass=DigestUtil.md5Hex(pass);\n" +
                            "        }\n" +
                            "        return decode.equals(char_count+pass+char_salt);\n" +
                            "    }\n" +
                            "\n" +
                            "}";

    public static void generate() throws IOException {
        String content = String.format(CLASS_TEMPLATE, LocalDate.now());
        File outputFile = new File(Easy.PROJECT_ABSOLUTE_URL + Easy.SRC_DIR + Easy.convertPackagePath(Easy.UTIL_PACKAGE) + "/" + CLASS_NAME + ".java");
        try (FileWriter writer = new FileWriter(outputFile)) {
            writer.write(content);
        }
    }
}
